package com.zhilianbao.leyaogo.view.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.PayConfig;

import java.util.List;

import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author: hcy
 * Description:选择支付方式
 * Created: 2016/11/3 9:43
 */
public class SelectPayMethodView extends FrameLayout {

    @BindViews({R.id.iv_wx_pay, R.id.iv_ali_pay, R.id.iv_my_wallet})
    public List<ImageView> mIvList;
    private int payMethod;

    private int[] mSelectImgResList = {R.drawable.pay_wechat_p, R.drawable.pay_alipay_p, R.drawable.pay_card_p},
            mNormalImgResList = {R.drawable.pay_wechat_n, R.drawable.pay_alipay_n, R.drawable.pay_card_n};

    public SelectPayMethodView(Context context) {
        this(context, null);
    }

    public SelectPayMethodView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_pay_type_select, this, false);
        ButterKnife.bind(this, view);
        addView(view);
    }

    @OnClick({R.id.fl_wx_pay, R.id.fl_ali_pay, R.id.fl_my_wallet})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_wx_pay:
                changState(0);
                payMethod = PayConfig.WX_PAY;
                break;
            case R.id.fl_ali_pay:
                changState(1);
                payMethod = PayConfig.ALI_PAY;
                break;
            case R.id.fl_my_wallet:
                changState(2);
                payMethod = PayConfig.YUE_PAY;
                break;
        }
    }

    /**
     * 改变ImageView
     */
    private void changState(int index) {
        for (int i = 0; i < mIvList.size(); i++) {
            if (i == index) {
                mIvList.get(i).setImageResource(mSelectImgResList[i]);
            } else {
                mIvList.get(i).setImageResource(mNormalImgResList[i]);
            }
        }
    }

    /**
     * 设置选择的支付方式
     *
     * @param payMethod
     */
    public void setPayMethod(int payMethod) {
        this.payMethod = payMethod;
        switch (payMethod) {
            case PayConfig.WX_PAY:
                changState(0);
                break;
            case PayConfig.ALI_PAY:
                changState(1);
                break;
            case PayConfig.YUE_PAY:
                changState(2);
                break;
            default:
                changState(-1);
                break;
        }
    }

    /**
     * 获取选择的支付方式
     *
     * @return
     */
    public int getPayMethod() {
        return payMethod;
    }
}
